
function Reader (string)
{
    this.read=read;

    var i = 0;
    var len = string.length;
    
    function next ()
    {
	if (i==len)
	    throw("End of string while reading");

	return string[i++];
    }

    function peek ()
    {
	return string[i];
    }

    function whiteSpace (c)
    {
	return c == " " || c == "\n";
    }

    function parenthesis(c)
    {
	return c == "(" || c == ")";
    }

    function seperator (c)
    {
	return whiteSpace(c) || parenthesis(c);
    }

    function readToken()
    {
	var token="";
	var c=next();

	while (whiteSpace(c))
	    c=next();

	if (parenthesis(c))
	    return c;

	while (!seperator(peek())) 
	    token += next();

	return token;
    }

    function read()
    {
	var token = readToken();

	if (token == ")")
	    throw("Misbalanced parenthesis");

	if (token == "(")
	    return readList();

	return makeAtom(token);
    }

    function readList()
    {
	var token = readToken();
	var exp;

	if (token == ")")
	    return null;

	if (token == "(")
	    exp = readList();
	else
	    exp = makeAtom(token);

	return cons(exp,readList());
    }

}
